//
//  RESingleton.m
//  录音
//
//  Created by ywy on 2016/12/29.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "RESingleton.h"

@implementation RESingleton
{
    //      数据库的非标准单例的对象
    FMDatabase * _database;
}
//static  RESingleton * singlenton = nil;
+(RESingleton*)defaultCent
{
    static RESingleton *manger = nil;
    @synchronized(self){
        if (manger == nil) {
            manger = [[self alloc] init];
        }
    }
    return manger;
}
// 初始化 数据库
- (id)init
{
    if (self = [super init]) {
        // 获得数据库的文件的
        NSString * filePath = [self  getFileFullPathWithFile:@"App.db"];
        // 创建_database
        _database = [[FMDatabase alloc] initWithPath:filePath];
        // open 打开
        if ([_database open]) {
            NSLog(@"数据库打开成功");
            
            [self creatTable];
        }else
        {
            NSLog(@"dabase  open error:%@",_database.lastError);
        }
    }
    return self;
}

/*
 //是否上传
 @property(nonatomic,assign) BOOL isUpPOst;
 // 是否归类到专辑
 @property(nonatomic,assign) BOOL isClassifiedAlbum;
 // 专辑的名字
 @property(nonatomic,copy) NSString *albumName;
 
 // 一级分类
 @property(nonatomic,copy)NSString * firstId;
 // 二级分类
 @property(nonatomic,copy)NSString * secondId;
 // 三级分类
 @property(nonatomic,copy)NSString * thirdId;
 
 
 */
// 创建数据库的表
- (void)creatTable
{
    NSString * sql = @"create table if not exists appInfo(serial integer  Primary Key Autoincrement,titleStr Varchar(256),baseUrl Varchar(256),date Varchar(256),currentTime Varchar(256),albumName Varchar(256),isUpPost Varchar(256),isClas Varchar(256),firstStr Varchar(256),secondStr Varchar(256),thirdStr Varchar(256),idStr Varchar(256),isSelected Varchar(256))";
    // ,isUpPost Varchar(256),isClassifiedAlbum Varchar(256),albumName Varchar(256)
    BOOL isSuccees = [_database executeUpdate:sql];
    if (!isSuccees) {
        
        NSLog(@"creatTable error:%@",_database.lastError);
        
        
    }
}

// 获取文件的全路径
- (NSString *)getFileFullPathWithFile:(NSString*)fileName
{
    NSString * docPath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents"];
    NSFileManager * fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:docPath]) {
        
        
        return [docPath  stringByAppendingFormat:@"/%@",fileName];
    }else
    {
        // 不存的时候 可以创建一个新的文件
        NSLog(@"Documents不存在");
        return nil;
    }
}
/*
 //是否上传
 @property(nonatomic,assign) BOOL isUpPOst;
 // 是否归类到专辑
 @property(nonatomic,assign) BOOL isClassifiedAlbum;
 // 专辑的名字
 @property(nonatomic,copy) NSString *albumName;
 */

-(NSArray*)readModel
{
    // 降序排列 查找的时候 
    FMResultSet *rs = [_database executeQuery:@"select * from  appInfo "];
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    while ([rs next]) {
        ReRecordModel*model = [[ReRecordModel alloc] init];
        model.titleStr = [rs stringForColumn:@"titleStr"];
        model.baseUrl = [rs stringForColumn:@"baseUrl"];
        model.date = [rs stringForColumn:@"date"];
        model.currentTime = [rs stringForColumn:@"currentTime"];
        model.isUpPost = [rs stringForColumn:@"isUpPost"];
        model.isClas = [rs stringForColumn:@"isClas"];
        model.albumName = [rs stringForColumn:@"albumName"];
        model.firstStr = [rs stringForColumn:@"firstStr"];
        model.secondStr = [rs stringForColumn:@"secondStr"];
        model.thirdStr = [rs stringForColumn:@"thirdStr"];
        model.idStr = [rs stringForColumn:@"idStr"];
        model.isSelected = [rs stringForColumn:@"isSelected"];
        [arr addObject:model];
    }
    
    return arr;
}
/*
 // 一级分类
 @property(nonatomic,copy)NSString * firstId;
 // 二级分类
 @property(nonatomic,copy)NSString * secondId;
 // 三级分类
 @property(nonatomic,copy)NSString * thirdId;
 **/
// 增加model 的数据的
-(void)insertModel:(ReRecordModel *)model
{
    if ([self isExistAppForTitle:model.baseUrl]) {
        NSLog(@"this app has Recored");
        return;
    }
    
    NSString * sql = @"insert into appInfo(titleStr,baseUrl,date,currentTime,albumName,isUpPost,isClas,firstStr,secondStr,thirdStr,idStr,isSelected) values(?,?,?,?,?,?,?,?,?,?,?,?)";
    BOOL isSuccess = [_database executeUpdate:sql,model.titleStr,model.baseUrl,model.date,model.currentTime,model.albumName,model.isUpPost,model.isClas,model.firstStr,model.secondStr,model.thirdStr,model.idStr,model.isSelected];
    
    if (!isSuccess) {
        NSLog(@"insert error:%@",_database.lastErrorMessage);
    }
}

- (void)deleteModel:(NSString*)title
{
    NSString * sql =@"delete from appInfo where baseUrl = ? ";
    BOOL isSuccess = [_database executeUpdate:sql,title];
    if (!isSuccess) {
        NSLog(@"delete error:%@",_database.lastErrorMessage);
    }
}


// 查找model
/*
 //是否上传
 @property(nonatomic,assign) BOOL isUpPOst;
 // 是否归类到专辑
 @property(nonatomic,assign) BOOL isClassifiedAlbum;
 // 专辑的名字
 @property(nonatomic,copy) NSString *albumName;
 // 一级分类
 @property(nonatomic,copy)NSString * firstId;
 // 二级分类
 @property(nonatomic,copy)NSString * secondId;
 // 三级分类
 @property(nonatomic,copy)NSString * thirdId;
 */
- (NSArray *)readModelWithDate:(NSString*)dateStr
{
    FMResultSet *rs = [_database executeQuery:@"select * from  appInfo where date = ? order by serial DESC",dateStr];
    
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    while ([rs next]) {
        ReRecordModel*model = [[ReRecordModel alloc] init];
        model.titleStr = [rs stringForColumn:@"titleStr"];
        model.baseUrl = [rs stringForColumn:@"baseUrl"];
        model.date = [rs stringForColumn:@"date"];
        model.currentTime = [rs stringForColumn:@"currentTime"];
        model.isUpPost = [rs stringForColumn:@"isUpPost"];
        model.isClas = [rs stringForColumn:@"isClas"];
        model.albumName = [rs stringForColumn:@"albumName"];
        model.firstStr = [rs stringForColumn:@"firstStr"];
        model.secondStr = [rs stringForColumn:@"secondStr"];
        model.thirdStr = [rs stringForColumn:@"thirdStr"];
        model.idStr = [rs stringForColumn:@"idStr"];
         model.isSelected = [rs stringForColumn:@"isSelected"];
        [arr addObject:model];
    }
    return arr;
}

// 根据专辑的名字
-(NSArray *)readModelWithAlbum:(NSString*)albumStr
{
    FMResultSet *rs = [_database executeQuery:@"select * from  appInfo where albumName = ? order by serial DESC",albumStr];
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    while ([rs next]) {
        ReRecordModel*model = [[ReRecordModel alloc] init];
        model.titleStr = [rs stringForColumn:@"titleStr"];
        model.baseUrl = [rs stringForColumn:@"baseUrl"];
        model.date = [rs stringForColumn:@"date"];
        model.currentTime = [rs stringForColumn:@"currentTime"];
        model.isUpPost = [rs stringForColumn:@"isUpPost"];
        model.isClas = [rs stringForColumn:@"isClas"];
        model.albumName = [rs stringForColumn:@"albumName"];
        model.firstStr = [rs stringForColumn:@"firstStr"];
        model.secondStr = [rs stringForColumn:@"secondStr"];
        model.thirdStr = [rs stringForColumn:@"thirdStr"];
        model.idStr = [rs stringForColumn:@"idStr"];
         model.isSelected = [rs stringForColumn:@"isSelected"];
        [arr addObject:model];
        
    }
    return arr;

}
// 根据日期查找
-(NSArray*)readModelWithTitle:(NSString *)titleStr
{
    //    NSString * sql = @"select * from appInfo order by serial DESC";//降序的排列  查找的时候
    //    NSString * sql = [NSString stringWithFormat:@"select * from  App",dateStr];
    FMResultSet *rs = [_database executeQuery:@"select * from  appInfo where titleStr = ? order by serial DESC",titleStr];
    NSMutableArray * arr = [[NSMutableArray alloc] init];
    while ([rs next]) {
        ReRecordModel*model = [[ReRecordModel alloc] init];
        model.titleStr = [rs stringForColumn:@"titleStr"];
        model.baseUrl = [rs stringForColumn:@"baseUrl"];
        model.date = [rs stringForColumn:@"date"];
        model.currentTime = [rs stringForColumn:@"currentTime"];
        model.isUpPost = [rs stringForColumn:@"isUpPost"];
        model.isClas = [rs stringForColumn:@"isClas"];
        model.albumName = [rs stringForColumn:@"albumName"];
        model.firstStr = [rs stringForColumn:@"firstStr"];
        model.secondStr = [rs stringForColumn:@"secondStr"];
        model.thirdStr = [rs stringForColumn:@"thirdStr"];
        model.idStr = [rs stringForColumn:@"idStr"];
         model.isSelected = [rs stringForColumn:@"isSelected"];
        [arr addObject:model];
    }
    return arr;

}


// 根据model的题目来查看是否存在

- (BOOL)isExistAppForTitle:(NSString *)baseUrl
{
    NSString * sql = @"select * from appInfo where baseUrl = ?";
    FMResultSet *rs = [_database executeQuery:sql,baseUrl];
    if ([rs next]) {
        return YES;
    }
    else
    {
        return NO;
    }
}
/*
 
 // 标题  主键
 @property (nonatomic, copy )NSString *title;
 //沙盒文件路径
 @property (nonatomic, copy) NSString  *baseUrl;
 // 总时间
 @property (nonatomic, copy  )NSString *currentTime;
 // 日期
 @property (nonatomic, copy ) NSString *date;
 //是否上传
 @property(nonatomic,copy) NSString * isUpPost;
 // 是否归类到专辑 isclassifiedalbum
 @property(nonatomic,copy) NSString * isClas;
 // 专辑的名字
 @property(nonatomic,copy) NSString *albumName;
 
 // 一级分类
 @property(nonatomic,copy)NSString * firstStr;
 // 二级分类
 @property(nonatomic,copy)NSString * secondStr;
 
 */

// 修改数据的信息
-(BOOL)ModifyTheDateWithBaseUrl:(NSString * )baseUrl
                       firstStr:(NSString *)firstis
                      secondStr:(NSString*)secondId
                       thirdStr:(NSString *)thirdId
                      albumName:(NSString*)albumName
                       titleStr:(NSString*)titleStr
{
    NSString * update = [NSString stringWithFormat:@"UPDATE appInfo SET firstStr = '%@',secondStr = '%@', thirdStr = '%@', albumName = '%@' , titleStr = '%@' ,isClas = '%@' WHERE baseUrl = '%@'",firstis,secondId,thirdId,albumName,titleStr,@"class",baseUrl];
    // title = '%@'
    BOOL isSuccess = [_database executeUpdate:update];
    if (!isSuccess) {
        NSLog(@"update Failure %@",_database.lastError);
    }
    return isSuccess;
}
-(BOOL)isUp:(NSString*)baseUrl
{
    NSString * update = [NSString stringWithFormat:@"UPDATE appInfo SET isUpPost = '%@' WHERE baseUrl = '%@'",@"isUP",baseUrl];
    // title = '%@'
    BOOL isSuccess = [_database executeUpdate:update];
    if (!isSuccess) {
        NSLog(@"update Failure %@",_database.lastError);
    }
    return isSuccess;
}
-(BOOL)isNotUP:(NSString *)baseUrl
{
    NSString * update = [NSString stringWithFormat:@"UPDATE appInfo SET isUpPost = '%@' WHERE baseUrl = '%@'",@"noUp",baseUrl];
    // title = '%@'
    BOOL isSuccess = [_database executeUpdate:update];
    if (!isSuccess) {
        NSLog(@"update Failure %@",_database.lastError);
    }
    return isSuccess;
}

-(BOOL)isIdStr:(NSString*)baseUrl with:(NSString *)idStr;
{
    NSString * update = [NSString stringWithFormat:@"UPDATE appInfo SET idStr = '%@' WHERE baseUrl = '%@'",idStr,baseUrl];
    // title = '%@'
    BOOL isSuccess = [_database executeUpdate:update];
    if (!isSuccess) {
        NSLog(@"update Failure %@",_database.lastError);
    }
    return isSuccess;
}
-(BOOL)isSelected:(NSString *)baseUrl
{
    NSString * update = [NSString stringWithFormat:@"UPDATE appInfo SET isSelected = '%@' WHERE baseUrl = '%@'",@"1",baseUrl];
    // title = '%@'
    BOOL isSuccess = [_database executeUpdate:update];
    if (!isSuccess) {
        NSLog(@"update Failure %@",_database.lastError);
    }
    return isSuccess;
}
-(BOOL)isNotSelected:(NSString *)baseUrl
{
    NSString * update = [NSString stringWithFormat:@"UPDATE appInfo SET isSelected = '%@' WHERE baseUrl = '%@'",@"0",baseUrl];
    // title = '%@'
    BOOL isSuccess = [_database executeUpdate:update];
    if (!isSuccess) {
        NSLog(@"update Failure %@",_database.lastError);
    }
    return isSuccess;
}




@end
